package org.hncors.gnssparse;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

import com.southgnss.gnssparse.GnssSateInfo;

public class GnssSatelliteInfoView extends View {
	// 卫星列表
	private List<GnssSateInfo> mgpsSatsList = null;

	private Paint m_circlePaint;
	private Paint m_textPaint;
	private Paint m_markerPaint;

	private int m_textHeight;

	private boolean mbDisplayGps = true;
	private boolean mbDisplayBd = true;
	private boolean mbDisplayGlonass = true;
	private boolean mbDisplayGalileo = true;

	public void setSateDisplayGps(boolean bDisplayGps) {
		mbDisplayGps = bDisplayGps;
		invalidate();
	}

	public void setSateDisplayBd(boolean bDisplayBd) {
		mbDisplayBd = bDisplayBd;
		invalidate();
	}

	public void setSateDisplayGlonass(boolean bDisplayGlonass) {
		mbDisplayGlonass = bDisplayGlonass;
		invalidate();
	}

	public void setSateDisplayGalileo(boolean bDisplayGalileo) {
		mbDisplayGalileo = bDisplayGalileo;
		invalidate();
	}

	// 构造函数
	public GnssSatelliteInfoView(Context context) {
		super(context);

		// 初始化界面
		initGpsView();
	}

	public GnssSatelliteInfoView(Context context, AttributeSet attrs) {
		super(context, attrs);

		// 初始化界面
		initGpsView();
	}

	public GnssSatelliteInfoView(Context context, AttributeSet ats,
								 int defaultStyle) {
		super(context, ats, defaultStyle);

		// 初始化界面
		initGpsView();
	}

	// 画圆底图
	private void onDrawCircle(Canvas canvas, float cx, float cy, float radius) {
		// Draw the background
		canvas.drawCircle(cx, cy, radius, m_circlePaint); // 外部圆

		// 画里面的虚线圆
		float radius_insideCircle = radius / 3;
		for (int j = 0; j < 2; j++) {
			canvas.drawCircle(cx, cy, radius_insideCircle, m_markerPaint);
			radius_insideCircle *= 2;
		}
		//这里需要save一次,否则后面画图会偏移
		canvas.save();
		// 画虚线
		for (int i = 0; i < 24; i++) {
			// Draw a marker.
			canvas.save();
			// canvas.translate(0, m_textHeight);
			if (i % 2 == 0) {
				// Draw the text every alternate 45deg
				String angle = String.valueOf(i * 15);
				float angleTextWidth = m_textPaint.measureText(angle);
				float angleTextX = cx - angleTextWidth / 2;
				float angleTextY = cy - radius + m_textHeight;
				canvas.drawText(angle, angleTextX, angleTextY, m_textPaint);
				// 画线
				Path path = new Path();
				path.moveTo(cx, cy - radius + m_textHeight);
				path.lineTo(cx, cy + radius - m_textHeight);
				canvas.drawPath(path, m_markerPaint);

			}
			canvas.restore();
			canvas.rotate(15, cx, cy);
		}
		canvas.restore();
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	// 画图函数
	@Override
	protected void onDraw(Canvas canvas) {
		// 获取组件宽度和高度
		float iCenterX = getMeasuredWidth() / 2;
		float iCenterY = getMeasuredHeight() / 2;
		// 画圆半径
		float radius = Math.min(iCenterX, iCenterY)
				- dip2px(this.getContext(), 10.0f)/** 7/8 */
				;

		onDrawCircle(canvas, iCenterX, iCenterY, radius);

		if (mgpsSatsList == null) {
			return;
		}

		float fPrnSize = radius / 10;
		Paint temPaint = new Paint();
		// 绘画信噪比
		GnssSateInfo temSatellite;
		for (int i = 0; i < mgpsSatsList.size(); i++) {
			temSatellite = mgpsSatsList.get(i);
			if (!mbDisplayGps && temSatellite.getPrn() < 33) {
				continue;
			}
			if (!mbDisplayBd && temSatellite.getPrn() >= 161
					&& temSatellite.getPrn() <= 198) {
				continue;
			}
			if (!mbDisplayGlonass && temSatellite.getPrn() >= 38
					&& temSatellite.getPrn() <= 69) {
				continue;
			}
			if (!mbDisplayGalileo) {
				continue;
			}

			// 计算卫星位置
			double dRate = 0;
			dRate = temSatellite.getElevation() / 90.0;
			float xdown = (int) (radius
					* Math.sin(temSatellite.getAzimuth() * Math.PI / 180.0) * (1 - dRate));
			float ydown = (int) (radius
					* Math.cos(temSatellite.getAzimuth() * Math.PI / 180.0) * (1 - dRate));

			// 锁定卫星显示绿色
			if (temSatellite.getSnr1() >= 25) {
				temPaint.setColor(Color.GREEN);
			} else {
				temPaint.setColor(Color.RED);
			}

			temPaint.setAntiAlias(true);
			canvas.drawCircle(iCenterX + xdown, iCenterY - ydown, fPrnSize,
					temPaint);

			temPaint.setColor(Color.BLACK);
			float textSize = (float) (fPrnSize * 1.0);
			temPaint.setTextSize(textSize);
			// 显示卫星号
			String strText = String.valueOf(temSatellite.getPrn());
			canvas.drawText(strText,
					iCenterX + xdown - temPaint.measureText(strText) / 2,
					iCenterY - ydown + 10, temPaint);

		}
	}// onDraw

	// 初始化界面
	protected void initGpsView() {
		//
		setFocusable(true);

		// 获取资源
		m_circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		m_circlePaint.setColor(getResources()
				.getColor(R.color.background_color));
		m_circlePaint.setStrokeWidth(1);
		m_circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);

		m_textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		m_textPaint.setTextSize(18);
		m_textPaint.setColor(getResources().getColor(R.color.text_color));

		m_textHeight = (int) m_textPaint.measureText("yY");

		m_markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		m_markerPaint.setColor(getResources().getColor(R.color.marker_color));

		// 设置虚线
		m_markerPaint.setAntiAlias(true);
		m_markerPaint.setStyle(Paint.Style.STROKE);
		PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
		m_markerPaint.setPathEffect(effects);

	}

	// 设置卫星数据
	public void setGpsSatellitesList(List<GnssSateInfo> list) {
		mgpsSatsList = list;
		invalidate();
	}

}
